package org.zxyj.manage.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zxyj.core.constants.Constants;
import org.zxyj.core.constants.Keys;
import org.zxyj.core.entity.Result;
import org.zxyj.core.exception.ServiceException;
import org.zxyj.core.web.controller.BaseController;
import org.zxyj.manage.entity.CourseObjectiveAnswer;
import org.zxyj.manage.service.ICourseObjectiveAnswerService;

/**
 * 设置课程客观题答案
    * @ClassName: CourseObjectiveAnswerController
    * @author liuyong
    * @date 2016年8月1日
 */
@Controller
@RequestMapping("/manage/courseObjectiveAnswer")
public class CourseObjectiveAnswerController extends BaseController{
	@Autowired
	private ICourseObjectiveAnswerService courseObjectiveAnswerService;
	
	@RequestMapping({"","/","/index"})
	public ModelAndView index(ModelAndView mv) {
		mv.setViewName(Views.LIST_VIEW);
		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("owner", getParaValue("owner"));
		mv.addObject(Keys.KEY_PARAM, courseObjectiveAnswerService.listEntities(criteria));
		mv.addObject("owner", getParaValue("owner"));
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/edit/exec")
	public Result<?> doEdit(@RequestBody Map<String,Object> criteria) throws Exception {
    	Result<?> result = new Result<String>();
    	try{
    		criteria.put("ids", Arrays.asList(criteria.get("ids").toString().split(",")));
    		criteria.put("strs", Arrays.asList(criteria.get("strs").toString().split(",")));
    		this.courseObjectiveAnswerService.saveOrUpdateEntity(criteria);
    		result.setResultCode(Constants.Common.DEFAULT_RESULT_CODE);
		} catch (ServiceException e) {
			result.setResultCode(e.getErrorNo());
		}
    	return result;
	}
	
	@ResponseBody
	@RequestMapping("/delete/exec")
	public Result<?> doDelete(CourseObjectiveAnswer entity) {
		Result<?> result = new Result<String>();
		try {
        	List<String> list = new ArrayList<String>();
        	list.add(entity.getId());
			this.courseObjectiveAnswerService.deleteEntity(list);
			result.setResultCode(Constants.Common.DEFAULT_RESULT_CODE);
		} catch (ServiceException e) {
			result.setResultCode(e.getErrorNo());
		}
		return result;
	}
	
	class Views {
		/**
		 * index视图
		 */
		public static final String LIST_INDEX = "/manage/courseObjectiveAnswer/list";
		
		/**
		 * 列表视图
		 */
		public static final String LIST_VIEW = "/manage/courseObjectiveAnswer/list";

		/**
		 * 添加视图
		 */
		public static final String ADD_VIEW = "/manage/courseObjectiveAnswer/edit";

		/**
		 * 编辑视图
		 */
		public static final String EDIT_VIEW = "/manage/courseObjectiveAnswer/edit";
	}
}
